package com.song.osclient;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.song.osclient.bean.MessageInfo;
import com.song.osclient.util.PrintLog;
import com.song.osclient.view.PrintToast;
import com.test.library.TestActivity;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by song on 2015/7/15.
 */
public class AppStartActivity extends Activity {
    List<MessageInfo> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                anim2MainActivity();
            }
        }, 1500);


//        createLink();
//        test();
//        open();


//        String nameByNum = getNameByNum("18603851392");
//        Toast.makeText(this, nameByNum, Toast.LENGTH_LONG).show();
    }


    private void anim2MainActivity() {
        Intent intent = new Intent(AppStartActivity.this, MainActivity.class);
        startActivity(intent);
        this.finish();
        overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
    }

    //========================================以下为test方法=====================================================
    public void createLink() {
        Intent intent = new Intent();
        intent.setClass(this, getClass());
        //以下两句是为了在卸载应用的时候同时删除桌面快捷方式
        intent.setAction("android.intent.action.MAIN");
        intent.addCategory("android.intent.category.LAUNCHER");
        Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
        shortcut.putExtra("duplicate", false);
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_launcher));
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
        sendBroadcast(shortcut);
    }
//    void test11() {
//        if (Build.VERSION.SDK_INT > 18) {
//            Window window = getWindow();
//            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//            // 创建状态栏的管理实例
//            SystemBarTintManager tintManager = new SystemBarTintManager(this);
//            // 激活状态栏设置
//            tintManager.setStatusBarTintEnabled(true);
//            // 激活导航栏设置
//            tintManager.setNavigationBarTintEnabled(true);
//            // 设置一个颜色给系统栏
//            tintManager.setTintColor(Color.parseColor("#FFFF6666"));
//        }
//    }

    void test() {
//        setContentView(R.layout.test);

    }

    /**
     * 设置Selector。
     */
    public static StateListDrawable makeSelector(Context context, int idNormal, int idPressed, int idFocused) {
        StateListDrawable bg = new StateListDrawable();
        Drawable normal = context.getResources().getDrawable(idNormal);
        Drawable pressed = context.getResources().getDrawable(idPressed);
        Drawable focused = context.getResources().getDrawable(idFocused);
        bg.addState(new int[]{android.R.attr.state_pressed,}, pressed);
        bg.addState(new int[]{android.R.attr.state_focused}, focused);
        bg.addState(new int[]{}, normal);
        return bg;
    }


    public void open() {
        String packageName = "com.ai.cmccha.crm";
        String className = "com.ai.cmccha.crm.EsopCallerActivity";

        try {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName(packageName, className));
            intent.putExtra("userName", "fanchensheng");//o
            intent.putExtra("pwd", "5tgb%TGB");//s
            intent.putExtra("account", "A123456");
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(AppStartActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }

    public List<MessageInfo> getAllMessageByNumber() {
        List<MessageInfo> list = new ArrayList<>();
        Uri uri = Uri.parse("content://sms/inbox");
        Cursor cursor = getContentResolver().query(uri, new String[]{"_id", "thread_id", "address", "body",}, "address=?", new String[]{"10086"}, null/*降序 升序把 desc 去掉即可*/);
        if (cursor == null) return list;
        while (cursor.moveToNext()) {
            int contentColumn = cursor.getColumnIndex("body");// 短信内容
            int idColumn = cursor.getColumnIndex("_id");// 短信内容
            MessageInfo messageInfo = new MessageInfo();
            messageInfo.setId(cursor.getInt(idColumn));
            messageInfo.setThreadId(cursor.getInt(cursor.getColumnIndex("thread_id")));
            messageInfo.setSmsContent(cursor.getString(contentColumn));
            list.add(messageInfo);
        }
        cursor.close();
        return list;
    }

    public void deleteSMS(String smsContent) {
        try {
            Uri uri = Uri.parse("content://sms/inbox");// 收信箱
            Cursor isRead = getContentResolver().query(uri, null, "read=" + 0, null, null);
            if (isRead == null) return;
            while (isRead.moveToNext()) {
                String body = isRead.getString(isRead.getColumnIndex("body")).trim();// 获取信息内容
                if (body.equals(smsContent)) {
                    int id = isRead.getInt(isRead.getColumnIndex("_id"));
                    getContentResolver().delete(Uri.parse("content://sms"), "_id=" + id, null);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void deleteSms(int id, int threadId) {
        getContentResolver().delete(Uri.parse("content://sms/inbox"), "_id=? or thread_id=?", new String[]{String.valueOf(id), String.valueOf(threadId)});
    }
}
